//
//  ZH_HomeSourceInfoViewController.m
//  WisdomStar
//
//  Created by zhangliwen on 2018/10/25.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "ZH_HomeSourceInfoViewController.h"
#import "ZH_ClassPersonTableViewCell.h"
#import "ZH_ClassContentTableViewCell.h"
#import "ZH_ClassButtonTableViewCell.h"
#import "ZH_ClassViewModel.h"
#import "ZH_ViedoInfomationViewController.h"
#import "ZH_ShoppingCarViewController.h"
#import "ZH_ConfirmOrdrViewController.h"
#import "ZH_VedioPlayerViewController.h"
@interface ZH_HomeSourceInfoViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,weak)UITableView *tableView;
@property(nonatomic,strong)ZH_ClassViewModel *viewModel;
@property (nonatomic,strong)NSString *typeString;
@property (nonatomic,strong)NSMutableArray *detailArray;

@end

@implementation ZH_HomeSourceInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUserInterface];
    //商品详情的数据
    [self initDetailData];
}
- (void)initUserInterface{
    self.navigationItem.title = @"课程详情";
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];
}
#pragma mark - initDetailData
- (void)initDetailData{
    [self.viewModel.courseDetailParam setObject:self.idString forKey:@"id"];
    WEAKSelf;
    [self.viewModel courseDetailResultBlock:^(NSString *code, NSString *message, id data) {
        if ([code isEqualToString:@"0"]) {
            [weakSelf.detailArray removeAllObjects];
            weakSelf.dic = data[@"data"];
            NSString *string = [NSString stringWithFormat:@"%@",self.dic[@"teach_type"]];
            if ([string isEqualToString:@"1"]) {
                self.typeString = @"一对一授课 ( 可购买 )";
            }
            else if([string isEqualToString:@"2"]){
                self.typeString = @"团体课程 ( 可购买 )";
            }
            NSArray *array1 = @[self.dic[@"create_at"],[NSString stringWithFormat:@"%@",self.dic[@"scan"]],self.typeString,self.dic[@"start"],@"2018-10-11 18: 05: 11",[NSString stringWithFormat:@"%@",self.dic[@"price"]],self.dic[@"address"]];
            [weakSelf.detailArray addObjectsFromArray:array1];
            NSLog(@"%@",weakSelf.detailArray);
            [weakSelf.tableView reloadData];
        }
        else{
            if ([code isEqualToString:@"1002"]) {
                [self showHint:@"登录过期"];
            }
            else{
                [weakSelf showHint:message];
            }
        }
    }];
}
#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 1) {
        return 7;
    }
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section == 1){
        return FITSCALE(44);
    }
    else if (section == 2){
        return FITSCALE(44);
    }
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return FITSCALE(9);
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        UIView *view = [UIView createViewWithBackgroundColor:kFontColorWhite];
        UIView *lineView = [UIView createViewWithBackgroundColor:[UIColor colorWithHexString:@"#00C9AE"]];
        [view addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(view);
            make.width.mas_equalTo(FITSCALE(4));
            make.height.mas_equalTo(FITSCALE(18));
            make.left.equalTo(view).offset(FITSCALE(11));
        }];
        UILabel *label = [UILabel createLabelWithText:@"课程详情" font:kFontSize_30];
        label.font = [UIFont boldSystemFontOfSize:15];
        [view addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(view);
            make.left.equalTo(lineView.mas_right).offset(FITSCALE(8));
        }];
        UIButton *button = [UIButton createNoBgButtonWithTitle:@"积分兑换: 500" target:self action:nil];
        [button setTitleColor:kFontColorWhite forState:UIControlStateNormal];
        button.backgroundColor = [UIColor colorWithHexString:@"#00C9AE"];
        button.layer.cornerRadius = FITSCALE(14);
        button.layer.masksToBounds = YES;
        button.clipsToBounds = YES;
        button.titleLabel.font = kFontSize_28;
        [view addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(view);
            make.right.equalTo(view).offset(FITSCALE(-12));
            make.height.mas_equalTo(FITSCALE(29));
            make.width.mas_equalTo(FITSCALE(101));
        }];
        UIView *lineViews = [UIView createViewWithBackgroundColor:[UIColor colorWithHexString:@"#DFDFDF"]];
        [view addSubview:lineViews];
        [lineViews mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(view);
            make.height.mas_equalTo(FITSCALE(1));
        }];
        return view;
    }
    else if (section == 2){
        UIView *view = [UIView createViewWithBackgroundColor:kFontColorWhite];
        UIView *lineViewss = [UIView createViewWithBackgroundColor:[UIColor colorWithHexString:@"#DFDFDF"]];
        [view addSubview:lineViewss];
        [lineViewss mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(view);
            make.height.mas_equalTo(FITSCALE(1));
        }];
        UIView *lineView = [UIView createViewWithBackgroundColor:[UIColor colorWithHexString:@"#00C9AE"]];
        [view addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(view);
            make.width.mas_equalTo(FITSCALE(4));
            make.height.mas_equalTo(FITSCALE(18));
            make.left.equalTo(view).offset(FITSCALE(11));
        }];
        UILabel *label = [UILabel createLabelWithText:@"课程简介" font:kFontSize_30];
        label.font = [UIFont boldSystemFontOfSize:15];
        [view addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(view);
            make.left.equalTo(lineView.mas_right).offset(FITSCALE(8));
        }];
        UIView *lineViews = [UIView createViewWithBackgroundColor:[UIColor colorWithHexString:@"#DFDFDF"]];
        [view addSubview:lineViews];
        [lineViews mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(view);
            make.height.mas_equalTo(FITSCALE(1));
        }];
        return view;
    }
    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        ZH_ClassPersonTableViewCell *cell = [ZH_ClassPersonTableViewCell cellWithTableView:tableView];
        [cell updataUI:self.dic];
        return cell;
    }
    else if (indexPath.section == 1){
        NSArray *array = @[@"发布时间:",@"浏览量:",@"授课方式:",@"开课时间:",@"结束时间:",@"原价:",@"上课地址:"];
        ZH_ClassContentTableViewCell *cell = [ZH_ClassContentTableViewCell cellWithTableView:tableView];
        cell.leftString = array[indexPath.row];
        if (self.detailArray.count > 0) {
             cell.contentString = self.detailArray[indexPath.row];
        }
        return cell;
    }
    else if (indexPath.section == 2){
        ZH_ClassButtonTableViewCell *cell = [ZH_ClassButtonTableViewCell cellWithTableView:tableView];
        [cell updataUI:self.dic];
        if ([self.typeStrings isEqualToString:@"1"]) {
            [cell updateHidden:YES];
        }
        WEAKSelf;
        cell.onButtonClick = ^(NSInteger tag) {
            if (tag == 1) {
                [self.viewModel.addCarParam setObject:self.idString forKey:@"course_id"];
                [self.viewModel addCarResultBlock:^(NSString *code, NSString *message, id data) {
                    if ([code isEqualToString:@"0"]) {
                        [weakSelf showHint:@"加入购物车成功"];
                        ZH_ShoppingCarViewController *vc = [[ZH_ShoppingCarViewController alloc] init];
                        vc.hidesBottomBarWhenPushed = YES;
                        [weakSelf.navigationController pushViewController:vc animated:YES];
                    }
                    else{
                        if ([code isEqualToString:@"1002"]) {
                            [weakSelf showHint:@"登录过期"];
                        }
                        else{
                            [weakSelf showHint:message];
                        }
                    }
                }];
            }
            else if (tag == 2){
                [self.viewModel.submitParam setObject:self.idString forKey:@"course_id"];
                [self.viewModel submitResultBlock:^(NSString *code, NSString *message, id data) {
                    if ([code isEqualToString:@"0"]) {
                        ZH_ConfirmOrdrViewController *vc = [[ZH_ConfirmOrdrViewController alloc] init];
                        vc.totlePrice = self.priceString;
                        vc.dic = self.dics;
                        vc.type = @"1";
                        vc.hidesBottomBarWhenPushed = YES;
                        [self.navigationController pushViewController:vc animated:YES];
                    }
                    else{
                        [weakSelf showHint:message];
                    }
                }];
            }
            else if (tag == 3){
                if([self.typeStrings isEqualToString:@"2"]){
                    ZH_VedioPlayerViewController *vc = [[ZH_VedioPlayerViewController alloc] init];
                    vc.hidesBottomBarWhenPushed = YES;
                    vc.mp4_url = @"http://120.79.164.26/buy/public/uploads/video/20180915/9eb1fba8dce30806c34a9a5d5a097fbb.mp4";
                    [self.navigationController pushViewController:vc animated:YES];
                }
                else{
                    ZH_ViedoInfomationViewController *vc = [[ZH_ViedoInfomationViewController alloc] init];
                    vc.hidesBottomBarWhenPushed = YES;
                    vc.nameString = self.nameString;
                    [self.navigationController pushViewController:vc animated:YES];
                }
            }
        };
        return cell;
    }
    return nil;
}
#pragma mark - getter,setter
- (UITableView *)tableView{
    if (!_tableView) {
        UITableView *tableView = [UITableView createTableViewWithDelegateTarget:self];
        tableView.backgroundColor = kCommonBackgroudColor;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [tableView setCellAutoAdaptationForEstimatedRowHeight:FITSCALE(100)];
        [self.view addSubview:_tableView = tableView];
    }
    return _tableView;
}
- (ZH_ClassViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[ZH_ClassViewModel alloc] init];
    }
    return _viewModel;
}
- (NSMutableArray *)detailArray{
    if (!_detailArray) {
        _detailArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _detailArray;
}
@end
